
import pygame
import logging
import io
import yaml


class World:
    def __init__(self):
        self.particleList=[]
       
        self.loadMap()
        #self.initParticle() 
        
        #self.robot=Robot(self,100,300,0,(0.3,0.3),(255,0,0),True)


    #def initParticle(self):
    #    for t in range(1,PARTICLE_COUNT):
    #        particle=Robot(self,random.randint(20,580),random.randint(20,580),random.uniform(0,math.pi*2),(0.3,0.3),(0,255,0)) 
    #        self.particleList.append(particle)
         

    def loadMap(self):
       
        mapYAML=self.loadMapFile()
        self.resolution=mapYAML['resolution']        
        
        self.loadMapImage(mapYAML['image'])    
    def loadMapImage(self,img):
        logging.info('Load image map : %s',img)
           
        self.mapImage = pygame.image.load(img)
       
        width,height=self.mapImage.get_size()
        
        self.mapData=[] 
        for i in range(0,height):
            a=[] 
            for j in range(0,width):
                if self.mapImage.get_at((i,j))==(255,255,255,255):
                   a.append(0)
                else:
                   a.append(1)
            self.mapData.append(a)   
 
        
         
                        
    def loadMapFile(self):
        
        logging.info("Load map file")
        try:
            fh=io.open('map.yaml') 
            try:
               mapYAML=yaml.load(fh)
            finally:
               fh.close() 
        except IOError:
           print "Error read file"

        return mapYAML
    
    def update(self,dt):
        #self.robot.update(dt)
        #for p in self.particleList:
        #    p.update(dt)   

        #print self.robot.x 
        #if self.mapData[self.robot.x][self.robot.y]==1 :
        #   print "a"              
        pass 

    def draw(self,surface):

        # draw background
        surface.blit(self.mapImage,(0,0))
        
